package com.zlp.mall.modules.pms.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlp.mall.common.api.Pager;
import com.zlp.mall.common.api.RespCode;
import com.zlp.mall.common.exception.CustomException;
import com.zlp.mall.common.util.BeanToUtils;
import com.zlp.mall.common.util.MethodUtil;
import com.zlp.mall.modules.pms.dto.resp.ProductAttributeCategoryItemResp;
import com.zlp.mall.modules.pms.dto.resp.ProductAttributeCategoryResp;
import com.zlp.mall.modules.pms.mapper.ProductAttributeCategoryMapper;
import com.zlp.mall.modules.pms.model.ProductAttribute;
import com.zlp.mall.modules.pms.model.ProductAttributeCategory;
import com.zlp.mall.modules.pms.service.ProductAttributeCategoryService;
import com.zlp.mall.modules.pms.service.ProductAttributeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 产品属性分类表 服务实现类
 * </p>
 *
 * @author ZouLiPing
 * @since 2021-09-23
 */
@Service
@Slf4j(topic = "ProductAttributeCategoryServiceImpl")
public class ProductAttributeCategoryServiceImpl extends ServiceImpl<ProductAttributeCategoryMapper, ProductAttributeCategory> implements ProductAttributeCategoryService {

    @Resource
    private ProductAttributeService productAttributeService;

    @Override
    public Pager<ProductAttributeCategoryResp> list(Integer pageNum, Integer pageSize) {

        log.info("list.req pageNum={},pageSize={}",pageNum,pageSize);
        IPage<ProductAttributeCategory> pages = new Page<>(pageNum, pageSize);
        IPage<ProductAttributeCategory> page = this.page(pages);
        List<ProductAttributeCategory> records = page.getRecords();
        if (CollectionUtils.isNotEmpty(records)) {
            List<ProductAttributeCategoryResp> productAttributeCategoryResps = BeanToUtils.entityToList(records, ProductAttributeCategoryResp.class);
            return new Pager(productAttributeCategoryResps,page.getTotal(), pageSize, pageNum);
        }
        return new Pager<>();
    }

    @Override
    public List<ProductAttributeCategoryItemResp> getListWithAttr() {

        log.info("getListWithAttr");
        List<ProductAttributeCategoryItemResp> productCategoryAttributes = new ArrayList<>();
        LambdaQueryWrapper<ProductAttributeCategory> queryWrapper = new LambdaQueryWrapper<>();
        List<ProductAttributeCategory> productAttributeCategories = this.list();
        if (CollectionUtil.isNotEmpty(productAttributeCategories)) {
            productCategoryAttributes = BeanToUtils.entityToList(productAttributeCategories, ProductAttributeCategoryItemResp.class);
            for (ProductAttributeCategoryItemResp productAttributeCategory : productCategoryAttributes) {
                List<ProductAttribute> productAttributes = productAttributeService.getProductAttrByProCatIdAndType(productAttributeCategory.getId(),1);
                if (CollectionUtil.isNotEmpty(productAttributes)){
                    productAttributeCategory.setProductAttributeList(productAttributes);
                }
            }
        }
        return productCategoryAttributes;
    }

    @Override
    public boolean create(String name) {

        log.info("create.req name={}",name);
        ProductAttributeCategory entity = new ProductAttributeCategory();
        entity.setName(name);
        return this.save(entity);
    }

    @Override
    public boolean update(Long id, String name) {

        log.info("create.req id={},name={}",id,name);
        ProductAttributeCategory entity = new ProductAttributeCategory();
        entity.setId(id);
        entity.setName(name);
        return this.updateById(entity);
    }

    @Override
    public boolean delete(Long id) {

        log.info("create.req id={}",id);
        List<ProductAttribute> productAttributes = productAttributeService.getProductAttrByProCatId(id);
        if (CollectionUtil.isNotEmpty(productAttributes)) {
            throw new CustomException(RespCode.BACK_PMS_3004, MethodUtil.getLineInfo());
        }
        return this.removeById(id);
    }

    @Override
    public ProductAttributeCategoryResp getItem(Long id) {

        log.info("create.req id={}",id);
        ProductAttributeCategory attributeCategory = this.getById(id);
        return  BeanToUtils.doToDto(attributeCategory,ProductAttributeCategoryResp.class);
    }
}
